package com.trw.trp.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.trw.trp.bean.MailBean;
import com.utuan.log.Log;
import com.utuan.log.LogFactory;

public class MailDao extends AbstractDao {

	private static Log log = LogFactory.getLog(MailDao.class);

	private static final String SQL_GET_MAIL = "SELECT mail_id,subject,body FROM trp.mail_template where mail_id=?";

	public MailBean getMail(long mailId) {
		MailBean bean = null;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = getConnection();
			ps = conn.prepareStatement(SQL_GET_MAIL);
			ps.setLong(1, mailId);
			rs = ps.executeQuery();
			if (rs.next()) {
				bean = new MailBean();
				bean.setMailId(rs.getLong(1));
				bean.setSubject(rs.getString(2));
				bean.setBody(rs.getString(3));
			}
		} catch (SQLException e) {
			log.warn(e);
			try {
				conn.rollback();
			} catch (SQLException e1) {
				log.warn(e1);
			}
		} finally {
			close(conn, rs, ps);
		}
		return bean;
	}

}
